<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/4/25
 * Time: 下午2:13
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class PosterSpaceController extends AdminBaseController{
    /**
     * 广告版位列表
     */
    public function index(){
        $list = M('posterspace') -> select();
        $this -> assign('list',$list);
        $this -> display();
    }
    public function pageList(){
        $page = I('get.page/d',1);

        $rule = M('rule');
        //  总记录数
        $count = $rule -> count();
        $limit = 20;
        //  总页数
        $pages = ceil($count/$limit);
        $start = empty($page - 1) ? 0 : (($page - 1) * $limit);

        $list = $rule -> order('id') -> limit($start . ',' . $limit) -> select();

        $list = get_tree($list, 0, 0, '----&nbsp;');
        $content = '';
//        foreach($list as $value){
//            $content .= '<tr><td>' . $value['id'] . '</td><td>' . $value['html'] . $value['title'] . '</td><td>' . $value['name'] . '</td><td>';
//            if($value['status']){
//                $content .= '<a href="javascript:statusChange(' . $value['id'] . ');" class="btn btn-outline btn-sm btn-primary" id="status' . $value['id'] . '">正常</a>';
//            }else{
//                $content .= '<a href="javascript:statusChange(' . $value['id'] . ');" class="btn btn-outline btn-sm btn-danger" id="status' . $value['id'] . '">禁用</a>';
//            }
//            $content .= '</td><td><div class="col-md-12"><a class="btn btn-sm btn-outline btn-warning" href="' . U('Admin/Rule/add/pid/' .$value['id']) . '"><i class="fa fa-edit"></i> 添加子权限</a><a class="btn btn-sm btn-outline btn-warning" href="' . U('Admin/Rule/edit/id/' .$value['id']) . '"><i class="fa fa-edit"></i> 修改</a><a class="btn btn-sm btn-outline btn-danger" href="javascript:del(' . $value['id'] . ')"><i class="fa fa-trash-o"></i> 删除</a></div></td></tr>';
//        }
        $data = [
            'pages' => $pages,
            'content' => $content,
        ];
        echo json_encode($data);
    }

    /**
     * 权限修改表单(与添加共用了同一个模版文件)
     */
    public function edit(){
        if(IS_POST){
            $id = I("post.id/d",0);
            if(!$id){
                $this->error('非法操作','/Admin/PosterSpace/index');
            }
            $postersapceInfo = M('posterspace')->field('id,name,type,width,height,description')->where(['id'=>$id])->find();
//            var_dump($postersapceInfo);exit;
            $this->ajaxReturn(['status' => 0, 'msg' => '', 'data' => $postersapceInfo]);
        }
    }

    /**
     * 修改及保存
     */
    public function Save(){
        if(IS_AJAX && IS_POST){
            //dump($_POST);exit;
            $id = I('post.id/d',0);
            $name = I("post.name/s",'');
            if(!$name){
                $this->ajaxReturn(['status' => 1, 'msg' => '版块名称不能为空！']);
            }
            $width = I("post.width/s",'');
//            dump($ask);exit;
            if(!is_int($width)){
                $this->ajaxReturn(['status' => 1, 'msg' => '尺寸宽不合要求！']);
            }
            $height = I("post.height/s",'');
            if(!is_int($height)){
                $this->ajaxReturn(['status' => 1, 'msg' => '尺寸高不合要求！']);
            }
            $type = I("post.type/d",0);
            $description = I("post.description/s",'');
            $strSql = '';
            $posterspace = M('posterspace');
            $data = $posterspace -> where(['name' => $name]) -> find();
            if(!empty($id) && data){
                $this -> ajaxReturn(['status' => 1, 'msg' => '该版块已经存在']);
            }
//            echo $posterspace;exit;
//
//            /**
//             *  id为空是添加 否则为修改
//             *  $titleArr 和 $nameArr对应 支持批量添加
//             */
//            if($titleArr && $nameArr && empty($id)){
//                foreach($nameArr as $key => $val){
//                    $strSql .= '('.$pid.',"'.$val.'","'.$titleArr[$key].'",'.$status.'),';
//                }
//                $strSql = rtrim($strSql,',');
//                $sql = 'insert into dx_rule(pid,name,title,status) VALUES '.$strSql;
//                $isSQl = $rule -> execute($sql);
//            }else{
//                $ruleNum = $rule->where(['title'=>$title,'name'=>$name])->count();
//                if($ruleNum > 0 && empty($id)){
//                    $this->ajaxReturn(['status' => 1, 'msg' => '权限已经存在！']);
//                }
//
                $data = [
                    'id' => $id,
                    'name' => $name,
                    'type'=> $type,
                    'width'=> $width,
                    'height'=> $height,
                    'description' => $description
                ];

                $isSQl = $posterspace -> add($data,[],true);
//            }
            if(!$isSQl){
                $this->ajaxReturn(['status' => 1, 'msg' => '失败！']);
            }
                $this->ajaxReturn(['status' => 0, 'msg' => '成功！']);
        }else{
            $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
        }
    }

    /**
     * 更改权限状态
     */
    public function statusChange(){
        if(IS_AJAX && IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
            }
            $rule = M('Rule');
            $r = $rule ->field('status') -> where(['id' => $id]) -> find();
            if(1 == $r['status']){
                $rule -> where(['id' => $id]) -> setField('status', 0);
                $this -> ajaxReturn(['status' => 0, 'msg' => '已禁用', 'data' => 0]);
            }else{
                $rule -> where(['id' => $id]) -> setField('status', 1);
                $this -> ajaxReturn(['status' => 0, 'msg' => '已开启', 'data' => 1]);
            }
        }else{
            $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
        }
    }

    /**
     * 删除权限
     */
    public function del(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作!']);
            }
            $isDel = M('posterspace')->where(['id' => $id])->delete();
            if(!$isDel){
                $this->ajaxReturn(['status' => 1 , 'msg' => '删除失败！']);
            }
            $this->ajaxReturn(['status' => 0 , 'msg' => '删除成功！']);
        }else{
            $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作!']);
        }
    }
}